package com.eugene.shiftscalendar;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by HOME on 15.03.14.
 */
public class Settings {

    static boolean showShifts;
    static boolean showCountryHolidays2014;
    static boolean useUserChanges;
    static Date shiftStart;
    static int shiftWorkDay;
    static int shiftRestDay;
    static String theme;

    static void loadSettings(Context context)
    {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        showShifts = sp.getBoolean(context.getString(R.string.StrPref_showShifts), true);
        showCountryHolidays2014 = sp.getBoolean(context.getString(R.string.StrPref_countryHolidays), false);
        useUserChanges = sp.getBoolean(context.getString(R.string.StrPref_userChanges), false);
        shiftWorkDay = Integer.valueOf(sp.getString(context.getString(R.string.StrPref_workDays), "2"));
        shiftRestDay = Integer.valueOf(sp.getString(context.getString(R.string.StrPref_restDays), "2"));
        theme = sp.getString(context.getString(R.string.StrPref_theme), "Glass");
        Date dt = new Date(114,0,1,1,1);
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        String mask = sp.getString(context.getString(R.string.StrPref_startDate), sdf.format(dt));
        try{
            shiftStart = DateInfo.initDate(sdf.parse(mask));
        } catch(Exception e){
            shiftStart = dt;
        }
    }

    static void saveSettings(Context context)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putBoolean(context.getString(R.string.StrPref_showShifts), showShifts)
                .putBoolean(context.getString(R.string.StrPref_countryHolidays), showCountryHolidays2014)
                .putBoolean(context.getString(R.string.StrPref_userChanges), useUserChanges)
                .putString(context.getString(R.string.StrPref_workDays), String.valueOf(shiftWorkDay))
                .putString(context.getString(R.string.StrPref_restDays), String.valueOf(shiftRestDay))
                .putString(context.getString(R.string.StrPref_theme), theme)
                .putString(context.getString(R.string.StrPref_startDate), sdf.format(shiftStart))
                .apply();
    }
}
